package com.jsb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.QuestionService;
import com.jsb.service.RedEnvelopesService;
import com.jsb.service.UserService;
import com.jsb.common.utils.StringUtils;
import com.jsb.dto.UserRedEnvelopes;
import com.jsb.model.Question;
import com.jsb.model.RedEnvelopes;
import com.jsb.model.RedEnvelopesExample;
import com.jsb.model.User;
import com.jsb.model.UserExample;

@Controller
public class RedEnvelopesController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/redEnvelopes/list.do";
	
	@Resource
	private RedEnvelopesService redEnvelopesService;
	@Resource
	private UserService userService;
	@Resource
	private QuestionService questionService;
	
	@RequestMapping("/redEnvelopes/list")
	public String list(
			Model model,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		RedEnvelopesExample redEnvelopesExample = new RedEnvelopesExample();
		if(StringUtils.isNotBlank(phone)){
			redEnvelopesExample.createCriteria().andSendPhoneNumLike("%"+phone+"%");
		}
		int totalCount = redEnvelopesService.countRedEnvelopes(redEnvelopesExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		redEnvelopesExample.setPageIndex((pageNum - 1) * pageSize);
		redEnvelopesExample.setPageSize(pageSize);
		redEnvelopesExample.setOrderByClause("sendDate desc");
		List<RedEnvelopes> redEnvelopess = redEnvelopesService.getPageRedEnvelopes(redEnvelopesExample);
		List<UserRedEnvelopes> userRedEnvelopess=new ArrayList<UserRedEnvelopes>();
		for (RedEnvelopes redEnvelopes : redEnvelopess) {
			UserRedEnvelopes userRedEnvelopes= new UserRedEnvelopes();
			userRedEnvelopes.setRedEnvelopes(redEnvelopes);
			
			String sendPhoneNum = redEnvelopes.getSendPhoneNum();
			UserExample sendUserExample = new UserExample();
			sendUserExample.createCriteria().andPhoneNumberEqualTo(sendPhoneNum);
			List<User> sendUsers = userService.selectByExample(sendUserExample);
			if(sendUsers !=null && sendUsers.size()>0){
				//将小对象封装到大对象中
				userRedEnvelopes.setSendUser(sendUsers.get(0));
			}
			String receiveUserPhoneNum = redEnvelopes.getUserPhoneNum();
			UserExample receiveUserExample = new UserExample();
			receiveUserExample.createCriteria().andPhoneNumberEqualTo(receiveUserPhoneNum);
			List<User> receiveUsers = userService.selectByExample(receiveUserExample);
			if(receiveUsers !=null && receiveUsers.size()>0){
				//将小对象封装到大对象中
				userRedEnvelopes.setReceiveUser(receiveUsers.get(0));
			}
			//封装问题对象
			String questionUuid = redEnvelopes.getQuestionUuid();
			Question questionByUuid = questionService.getQuestionByUuid(questionUuid);
			userRedEnvelopes.setQuestion(questionByUuid);
			
			userRedEnvelopess.add(userRedEnvelopes);
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("userRedEnvelopess", userRedEnvelopess);
		return "/RedEnvelopes/list";
	}
	
	@RequestMapping("/redEnvelopes/toAdd")
	public String toAdd(Model model) {
		return "/RedEnvelopes/add";
	}

	@RequestMapping("/redEnvelopes/add")
	public String add(Model model, @ModelAttribute("redEnvelopes") RedEnvelopes redEnvelopes) {
		redEnvelopesService.addRedEnvelopes(redEnvelopes);
		return redirect;
	}

	@RequestMapping("/redEnvelopes/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		redEnvelopesService.delRedEnvelopesByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/redEnvelopes/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		RedEnvelopes redEnvelopes = redEnvelopesService.getRedEnvelopesByUuid(uuid);
		model.addAttribute("redEnvelopes", redEnvelopes);
		return "/RedEnvelopes/edit";
	}

	@RequestMapping("/redEnvelopes/edit")
	public String edit(Model model, @ModelAttribute("redEnvelopes") RedEnvelopes redEnvelopes) {
		redEnvelopesService.updateRedEnvelopes(redEnvelopes);
		return redirect;
	}
	//查看红包的详情
	@RequestMapping("/redEnvelopes/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		UserRedEnvelopes userRedEnvelopes= new UserRedEnvelopes();
		RedEnvelopes redEnvelopes = redEnvelopesService.getRedEnvelopesByUuid(uuid);
		userRedEnvelopes.setRedEnvelopes(redEnvelopes);
		//收红包人
		String userPhoneNum = redEnvelopes.getUserPhoneNum();
		User receiveUser = userService.getUserByPhoneNumber(userPhoneNum);
		userRedEnvelopes.setReceiveUser(receiveUser);
		//发红包人
		String sendPhoneNum = redEnvelopes.getSendPhoneNum();
		User sendUser = userService.getUserByPhoneNumber(sendPhoneNum);
		userRedEnvelopes.setSendUser(sendUser);
		
		model.addAttribute("userRedEnvelopes", userRedEnvelopes);
		return "/RedEnvelopes/view";
	}
}
